<?php

namespace app\admin\model;

use think\Model;


class TeacherClassDtl extends Model
{

    

    

    // 表名
    protected $name = 'teacher_class_dtl';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text'
    ];



    public function getStatusList()
    {
        return ['1' => '未开始','2' => '已完成','3' => '已请假'];
    }

    public function getCycleList()
    {
        return ['1' => __('Cycle 1'),'2' => __('Cycle 2')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public static function numberToChinese($number) {
        $chineseMap = [
            1 => '一',
            2 => '二',
            3 => '三',
            4 => '四',
            5 => '五',
            6 => '六',
            7 => '天', // 7 改为 "天"
        ];

        return $chineseMap[$number] ?? $number; // 如果不在映射表，返回原数字
    }

    //确认进度
    public static function jd($teacher_class_dtl_id,$class_id,$class_num,$no_status = 0){
        if($no_status == 0){
            TeacherClassDtl::where(['id'=>$teacher_class_dtl_id])->update(['status'=>2]);
        }
        TeacherClass::where(['id'=>$class_id])->setInc('completed_class_num',$class_num);
        self::jd_send_notice($class_id);
    }

    public static function jd_send_notice($class_id){
        $class = TeacherClass::where(['id'=>$class_id])->with(['teachers','users'])->find();
        if($class){
            if($class['class_num_all'] <= $class['completed_class_num']&&$class['status']!=2){
                TeacherClass::where(['id'=>$class_id])->update(['status'=>2]);
                //给教师课程完成通知
                $send_add_notice = [];
                $school_name = isset($class['users']['nickname']) ? $class['users']['nickname'] : '';
                $send_add_notice[0]['title'] = '课程全部完成';//标题
                $send_add_notice[0]['content'] = $school_name."购买的课程已全部完成";//内容
                $send_add_notice[0]['need_confirm'] = 2;//是否需要确认 1是 2否
                $send_add_notice[0]['jump'] = '课程详情';//跳转地址
                $send_add_notice[0]['teacher_class_id'] = $class_id;//课程id
                $send_add_notice[0]['teacher_class_dtl_id'] = 0;//课程安排id
                $send_add_notice[0]['send_user_id'] = $class['user_id'];//发送人id
                $send_add_notice[0]['get_user_id'] = $class['teacher_id'];//接收人id
                $send_add_notice[0]['identity_type'] = '教师';//接收人身份
                $send_add_notice[0]['addtime'] = date('Y-m-d H:i:s');//添加时间
                $send_add_notice[0]['is_read'] = 1;//是否已读 1未读 2已读
                //给校方课程完成通知
                $teacher_name = isset($class['teachers']['nickname']) ? $class['teachers']['nickname'] : '';
                $send_add_notice[1]['title'] = '课程全部完成';//标题
                $send_add_notice[1]['content'] = $teacher_name."的课程已全部完成";//内容
                $send_add_notice[1]['need_confirm'] = 2;//是否需要确认 1是 2否
                $send_add_notice[1]['jump'] = '课程详情';//跳转地址
                $send_add_notice[1]['teacher_class_id'] = $class_id;//课程id
                $send_add_notice[1]['teacher_class_dtl_id'] = 0;//课程安排id
                $send_add_notice[1]['send_user_id'] = $class['teacher_id'];//发送人id
                $send_add_notice[1]['get_user_id'] = $class['user_id'];//接收人id
                $send_add_notice[1]['identity_type'] = '校方';//接收人身份
                $send_add_notice[1]['addtime'] = date('Y-m-d H:i:s');//添加时间
                $send_add_notice[1]['is_read'] = 1;//是否已读 1未读 2已读
                Notice::insertAll($send_add_notice);
                // 发送个推消息
                foreach ($send_add_notice as $notice) {
                    Notice::sendGetuiPush($notice);
                }
                //解除课程占用可约时间
                TeacherSubscribe::class_success_up($class_id,$class['teacher_id']);
            }
        }

    }

}
